package com.example.smartcampus;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Music extends AppCompatActivity {
    Button start, stop;
    ListView songName;
    TextView show_song;
    String songName1 = "彩云道月";
    String songFileName = "music01_cai";

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        getView();
        //创建适配器引入数组元素
//        最后一个参数是根据name找到合适的Array
        quoteArray();

//给列表添加点击事件
        clickList();
    }

    private void quoteArray() {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.list_item,
                getResources().getStringArray(R.array.songNameArray));

//        设置适配器
        songName.setAdapter(adapter);
    }

    private void start() {
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                添加一个意图传递给服务
                Intent intent = new Intent(Music.this, MyService.class);
                intent.putExtra("songFileName", songFileName);
                startService(intent);
                show_song.setText("正在播放:" + songName1);
            }
        });
    }

    private void clickList() {
        songName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                songName1 = ((TextView) view).getText().toString();
                Intent intent = new Intent(Music.this, MyService.class);
                switch (i) {
                    case 0:
                        songFileName = "music01_cai";
                        break;
                    case 1:
                        songFileName = "music02_bai";
                        break;
                    case 2:
                        songFileName = "music03_gao";
                        break;
                    case 3:
                        songFileName = "music04_liangzhu";
                        break;
                    case 4:
                        songFileName = "music05_yuzhou";
                        break;
                    default:
                        songFileName = "";
                }
//把数据通过intent传过去
                intent.putExtra("songFileName", songFileName);
                show_song.setText("正在播放:" + songName1);
                startService(intent);

            }
        });
        start();
        stop();
    }

    private void stop() {
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Music.this, MyService.class);
                stopService(intent);
                show_song.setText("");
            }
        });
    }

    private void getView() {
        start = findViewById(R.id.btn_start);
        stop = findViewById(R.id.btn_stop);
        songName = findViewById(R.id.songName);
        show_song = findViewById(R.id.show_song);
    }
}